<?php $this->headLink()->appendStylesheet($this->baseUrl() . '/css/search.css') ?>
<?php
$isSearchForm = !empty($this->searchForm);
if ($isSearchForm && method_exists($this->searchForm, 'hasAttributes')) {
    $isSearchForm = $this->searchForm->hasAttributes();
}
$action = My_Utils::getController()->getRequest()->getActionName();
?>
<div class="search">
    <?php /** / ?>
    <form class="form-search clearfix" action="/search/index" method="get">
        <div class="input-append span12">
            <input type="text" class="span9 search-query" name="q" value="<?= htmlspecialchars(My_Utils::getRequestParam('q')) ?>">
            <button type="submit" class="btn">Szukaj</button>
        </div> 
        <?php if ($isSearchForm): ?>
            <a class="search-adv-activator" href="#">Wyszukiwanie zaawansowane</a>
        <?php endif ?>
    </form>
    <?php /**/ ?>

    <?php if ($isSearchForm): ?>
        <div class="search-advanced">
            <?= $this->searchForm ?>
        </div>
    <?php endif ?>

    <ul class="nav nav-tabs" id="searchTabs">
        <li class="<?= $action == 'offer' ? 'active' : '' ?>"><a href="#offer">Oferty</a></li>
        <li class="<?= $action == 'catalog' ? 'active' : '' ?>"><a href="#catalog">Firmy i instytucje</a></li>
        <!--<li class="<?= $action == 'event' ? 'active' : '' ?>"><a href="#event">Repertuar kin i teatrów</a></li>-->
    </ul>

    <div class="tab-content" id="searchTabsResults">
        <div class="tab-pane <?= $action == 'offer' ? 'active' : '' ?>" id="offer"><?= $this->offerResult ?></div>
        <div class="tab-pane <?= $action == 'catalog' ? 'active' : '' ?>" id="catalog"><?= $this->catalogResult ?></div>
        <!--<div class="tab-pane <?= $action == 'event' ? 'active' : '' ?>" id="event"><?= $this->eventResult ?></div>-->
    </div>

</div>

<script>
    $(function() {

        $('.search').on('submit', 'form.form-search', function(event) {
            var q = $(this).find('.search-query').val();
            var tab = $('#searchTabsResults').find('.tab-pane.active');
            var tabId = tab.attr('id');
            var url = '/search/' + tabId;

            $(this).attr('action', url);
        });

        $('#searchTabs').on('click', 'a', function(e) {
            e.preventDefault();
            $(this).tab('show');
            $('.search form.form-search').submit();
        });

    });
</script>
<?php if ($isSearchForm): ?>
    <script type="text/javascript">
        $(function() {
            $('div.search form.offer-search-form').submit(function(event) {
                var q = $('div.search form.form-search .search-query').val();
                $('form.offer-search-form #q').val(q);
            });
            $('.search .search-adv-activator, .search .form-cancel').click(function(event) {
                event.preventDefault();
                $('.search .search-advanced').slideToggle();
                return false;
            });
        });
    </script>
    <style type="text/css">
        .search .form-search {
            margin-bottom: 0px;
        }
        .search .search-advanced {
            display: block;
        }
    </style>
<?php endif; ?>
